package unilearn.generate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.SQLException;
import unilearn.domain.Assignment;
import unilearn.domain.DataFile;
import unilearn.domain.Submission;
import unilearn.facade.SubmissionFacade;

/*
 * GenerateSubmission.java
 *
 * Created on 29 April 2008, 21:39
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 * @author Ben
 */
public class GenerateSubmission
{

	public static final String INSERT_SUBMISSION = "INSERT INTO submission (assignmentID,dateSubmitted,studentNumber,fileID) "
			+ "VALUES (%1,'2008-04-29','%2',%3);";

	private final PrintWriter writer;

	private PrintWriter createWriter;

	private final GenerateFileData fileGen;

	private long submissionsCreated = 0;

	/** Creates a new instance of GenerateSubmission */
	public GenerateSubmission(GenerateFileData fileGen, PrintWriter writer)
			throws FileNotFoundException
	{
		this.fileGen = fileGen;
		this.writer = writer;

		File outputFile = new File("submission_test_createdestroy.sql");
		FileOutputStream fos = new FileOutputStream(outputFile);
		createWriter = new PrintWriter(new OutputStreamWriter(fos));
		System.out.println("Exporting Create/Destroy TestCase to "
				+ outputFile.getAbsolutePath());
		createWriter.println("private Submission[] subs;");
		createWriter.println("private DataFile file;");
		createWriter.println("private Assignment assignment;");
		createWriter.println("private Submission result;");
		createWriter.println("private DataFile updateFile;");
		createWriter.println();
		createWriter.println("public void testSubmissions() throws SQLException {");
	}

	public void addSubmissions(long assignmentID, String[] subjectStudents)
		throws SQLException
	{
		/* Add Students */
		int created = 0;
		for (int i = 0; i < subjectStudents.length; i++)
		{
			int studentSubmitted = (int) (Math.random() * 7.0);
			if (studentSubmitted < 5)
			{
				created++;
				addSubmission(assignmentID, subjectStudents[i]);
			}
		}

		System.out.println(assignmentID + ":" + created);

		StringBuilder builder = new StringBuilder();

		// builder.append("public void testAssignment%1() throws SQLException
		// {\r\n");
		/* Create Submission */
		builder.append("assignment = new Assignment(%1);\r\n");
		builder.append("file = new DataFile(1);\r\n");
		builder.append("\r\n");

		builder.append("result = SubmissionFacade.createNewSubmission(assignment, file, \"123456Y\");\r\n");
		builder.append("if (result == null) fail(\"Submission not created\");");
		builder.append("assertEquals(%1, result.getAssignmentID());\r\n");
		builder.append("assertEquals(1, result.getFileID());\r\n");
		builder.append("// assertEquals(new Date(\"2008-04-29\"),result.getDateSubmitted());\r\n");
		builder.append("assertEquals(\"123456Y\",result.getStudentNumber());\r\n");
		builder.append("\r\n");

		/* Confirm Submission */
		builder.append("updateFile = new DataFile(2);");
		builder.append("subs = SubmissionFacade.getAssignmentSubmissions(%1);\r\n");
		builder.append("assertEquals(%2, subs.length);\r\n");
		builder.append("\r\n");

		/* Update Submission */
		builder.append("SubmissionFacade.updateSubmission(result, updateFile);\r\n");
		builder.append("assertEquals(2, result.getFileID());\r\n");
		builder.append("\r\n");

		/* Update Submission */
		builder.append("SubmissionFacade.updateSubmission(result, file);\r\n");
		builder.append("assertEquals(1, result.getFileID());\r\n");
		builder.append("\r\n");

		/* Delete Submission */
		builder.append("SubmissionFacade.deleteSubmission(result);\r\n");
		builder.append("subs = SubmissionFacade.getAssignmentSubmissions(%1);\r\n");
		builder.append("assertEquals(%3, subs.length);\r\n");
		builder.append("\r\n");

		// builder.append("}");

		String val = builder.toString();
		val = val.replace("%1", String.valueOf(assignmentID));
		val = val.replace("%2", String.valueOf(created + 1));
		val = val.replace("%3", String.valueOf(created));

		createWriter.print("/********** Assignment ");
		createWriter.print(assignmentID);
		createWriter.println(" **********/");
		createWriter.print(val);
		createWriter.println();
	}

	private long addSubmission(long assignmentID, String studentNumber)
	{
		submissionsCreated++;

		long fileID = fileGen.addFileData("submission", submissionsCreated);

		String value = INSERT_SUBMISSION;
		value = value.replace("%1", String.valueOf(assignmentID));
		value = value.replace("%2", studentNumber);
		value = value.replace("%3", String.valueOf(fileID));
		writer.println(value);

		return submissionsCreated;
	}

	public void close() throws IOException
	{
		createWriter.println("}");
		createWriter.close();
	}

}
